package reflect;

import reflect.annotations.AutoRunClass;

import java.io.File;
import java.io.FileFilter;

/**
 * @Classname: Test3
 * @Author: bromide
 * @CreateTime: 2022/4/19--10:27
 * @Version: V1.0
 * @Description: 自动化实例与当前类Test3在同一个包中被@AutoRunClass标注的类
 */
public class Test3 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test3.class.getResource(".").toURI());
        File[] files = dir.listFiles(file -> file.getName().endsWith(".class"));
        String packageName = Test3.class.getPackage().getName();
        for (File file : files) {
            String fileName = file.getName();
            String className = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName(packageName + "." + className);
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                Object obj = cls.newInstance();
                System.out.println("实例化:" + className);
            }
        }
    }
}
